<?php
// 应用公共文件


if (!function_exists('success')) {
	/**
	 * 请求成功
	 *
	 * @param array  $data
	 * @param int    $code
	 * @param string $message
	 *
	 * @param
	 *
	 * @return \think\response\Json
	 * @throws
	 *
	 */
	function success ($data = [], $code = \app\libs\enum\ApiCode::SUCCESS_CODE, $message = '操作成功') {
		return json(compact('code', 'message', 'data'));
	}
}

if (!function_exists('toast')) {
	/**
	 * 异常提示
	 *
	 * @param string $message
	 * @param int    $code
	 * @param array  $data
	 *
	 * @param
	 *
	 * @return \think\response\Json
	 * @throws
	 *
	 */
	function toast ($message = '请求失败', $code = 1, $data = []) {
		if (is_null($data)) {
			return json(compact('code', 'message'));
		}

		return json(compact('code', 'message', 'data'));
	}
}

if (!function_exists('okToast')) {
	/**
	 * 提示
	 *
	 * @param string $message
	 * @param int    $code
	 * @param array  $data
	 *
	 * @param
	 *
	 * @return \think\response\Json
	 * @throws
	 *
	 */
	function okToast ($message = '请求成功', $code = \app\libs\enum\ApiCode::SUCCESS_CODE, $data = []) {
		if (is_null($data)) {
			return json(compact('code', 'message'));
		}

		return json(compact('code', 'message', 'data'));
	}
}

if (!function_exists('random')) {
	function random ($length, $numeric = true) {
		$str = null;
		$mixture = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
		$number = '0123456789';
		$strBase = $numeric ? $number : $mixture;
		$max = strlen($strBase) - 1;

		for ($i = 0; $i < $length; $i++) {
			$str .= $strBase[rand(0, $max)];
		}

		return $str;
	}
}

if (!function_exists('incrCodeRule')) {
	/**
	 * @param string $id     id
	 * @param string $prefix 前缀
	 * @param int    $len    编码长度
	 *
	 * @param
	 *
	 * @return string
	 * @throws
	 *
	 */
	function incrCodeRule ($id = '00000', $prefix = 'C', $len = 7) {
		$newid = sprintf("%0" . $len . "d", intval($id) + 1);

		return $prefix . $newid;
	}
}

if (!function_exists('getHttps')) {
	//curl get请求
	function getHttps ($url, $header = null) {
		$curl = curl_init();

		if (!empty($header)) {
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}

		curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);

		return $output;
	}
}

if (!function_exists('postHttps')) {
	//curl get请求
	function postHttps ($url, $data = null, $header = null) {
		$curl = curl_init();

		if (!empty($header)) {
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}

		curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		//发送post 请求
		curl_setopt($curl, CURLOPT_POST, 1);
		//		if (!empty($data)) {
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		//		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);

		return $output;
	}
}

if (!function_exists('httpsPostJson')) {
	function httpsPostJson ($url, $jsonStr) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, [
			'Content-Type: application/json; charset=utf-8',
			'Content-Length: ' . strlen($jsonStr)
		]);
		$response = curl_exec($ch);
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);

		return [$httpCode, $response];
	}
}

if (!function_exists('downFile')) {
	/**
	 * 从指定服务器上下载音频
	 *
	 * @param        $url
	 * @param string $filename
	 * @param string $savePath
	 *
	 * @param
	 *
	 * @return bool|string
	 * @throws
	 *
	 */
	function downFile ($url, $filename = '', $savePath = '/') {
		$ch = curl_init();
		if (stripos($url, "https://") !== false) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		}
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, true);  //需要response header
		curl_setopt($ch, CURLOPT_NOBODY, false);  //需要response body
		$response = curl_exec($ch);
		//分离header与body
		$header = '';
		$body = '';
		if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
			$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //头信息size
			$header = substr($response, 0, $headerSize);
			$body = substr($response, $headerSize);
		}
		curl_close($ch);
		// print_r($header);die;
		//文件名
		$arr = [];
		if (!$filename) {
			preg_match('#filename="(.*?)"#', $header, $arr);
			$filename = $arr[1];
		}
		$fullName = rtrim($savePath, '/') . '/' . $filename;

		//判断目录是否存在|创建目录
		$basePath = dirname($fullName);
		if (!file_exists($basePath)) {
			mkdir($basePath, 0777, true);
		}
		//保存文件
		if (file_put_contents($fullName, $body)) {
			return $fullName;
		}

		return false;
	}
}

if (!function_exists('JsonEncode')) {

	/**
	 * json_encode
	 *
	 * @param     $data
	 * @param int $flags
	 * @param int $depth
	 *
	 * @param
	 *
	 * @return array|false|string
	 * @throws
	 *
	 */
	function JsonEncode ($data, $flags = JSON_UNESCAPED_UNICODE, $depth = 512) {
		if ($data instanceof \think\contract\Jsonable) {
			return (string) $data;
		}

		if ($data instanceof \think\contract\Arrayable) {
			return $data->toArray();
		}

		try {
			$json = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth);
		} catch (Throwable $exception) {
			throw new \think\exception\InvalidArgumentException($exception->getMessage(), $exception->getCode());
		}

		return $json;
	}


}

if (!function_exists('JsonDecode')) {

	/**
	 * json_decode
	 *
	 * @param      $json
	 * @param bool $assoc
	 * @param int  $depth
	 * @param int  $flags
	 *
	 * @param
	 *
	 * @return mixed
	 * @throws
	 *
	 */
	function JsonDecode ($json, $assoc = true, $depth = 512, $flags = 0) {
		try {
			$decode = json_decode($json, $assoc, $depth, $flags | JSON_THROW_ON_ERROR);
		} catch (Throwable $exception) {
			throw new \think\exception\InvalidArgumentException($exception->getMessage(), $exception->getMessage());
		}

		return $decode;
	}
}

if (!function_exists('incrCodeRule')) {
	/**
	 * @param string $code   code
	 * @param string $prefix 前缀
	 * @param int    $len    编码长度
	 *
	 * @param
	 *
	 * @return string
	 * @throws
	 *
	 */
	function incrCodeRule ($code = '0000000', $prefix = 'C', $len = 7) {
		$newCode = sprintf("%0" . $len . "d", intval($code) + 1);

		return $prefix . $newCode;
	}
}

if (!function_exists('logging')) {
	/**
	 * 记录日志
	 *
	 * @param string $channel
	 *
	 * @param
	 *
	 * @return \think\log\Channel|\think\log\ChannelSet
	 * @throws
	 *
	 */
	function logging ($channel = 'file') {
		return \think\facade\Log::channel($channel);
	}
}


if (!function_exists('getCurlyBracesString')) {
	/**
	 * @param      $ans_temp_text
	 * @param bool $is_original
	 *
	 * @param
	 *
	 * @return mixed
	 * @throws
	 *
	 */
	function getCurlyBracesString ($ans_temp_text, $is_original = false) {
		$reg = "~\{([^}]*)\}~";
		preg_match_all($reg, $ans_temp_text, $arr);

		return $is_original ? $arr[0] : $arr[1];
	}
}

if (!function_exists('getDollarCurlyBracesString')) {
	/**
	 * @param      $ans_temp_text
	 * @param bool $is_original
	 *
	 * @param
	 *
	 * @return mixed
	 * @throws
	 *
	 */
	function getDollarCurlyBracesString ($ans_temp_text, $is_original = false) {
		$reg = "~\\$\{([^}]*)\}~";
		preg_match_all($reg, $ans_temp_text, $arr);

		return $is_original ? $arr[0] : $arr[1];
	}
}


if (!function_exists('deleteFolder')) {
	/**
	 * 删除文件夹
	 *
	 * @param $folderPath
	 *
	 * @param
	 *
	 * @return bool
	 * @throws
	 *
	 */
	function deleteFolder ($folderPath) {
		if (is_dir($folderPath)) {
			$files = array_diff(scandir($folderPath), ['.', '..']);
			foreach ($files as $file) {
				$filePath = $folderPath . '/' . $file;
				if (is_dir($filePath)) {
					deleteFolder($filePath);
				} else {
					unlink($filePath);
				}
			}
			rmdir($folderPath);

			return true;
		}

		return false;
	}
}


if (!function_exists('deleteFolderFile')) {
	/**
	 * 删除文件夹
	 *
	 * @param $folderPath
	 *
	 * @param
	 *
	 * @return bool
	 * @throws
	 *
	 */
	function deleteFolderFile ($folderPath) {
		if (is_dir($folderPath)) {
			$p = scandir($folderPath);
			foreach ($p as $val) {
				if ($val != '.' && $val != "..") {
					unlink($folderPath . '/' . $val);
				}
			}
		}

		return true;
	}
}



